package com.pureut.system.controller;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.system.domain.MaterialCoding;
import com.pureut.system.domain.MaterialHalf;
import com.pureut.system.domain.dto.MaterialHalfDto;
import com.pureut.system.domain.vo.MaterialHalfVo;
import com.pureut.system.export.MaterialHalfExport;
import com.pureut.system.service.IMaterialCodingService;
import com.pureut.system.service.IMaterialHalfService;
import com.pureut.system.util.DownTemplateUtil;
import com.pureut.system.util.ExcelUtil;
import com.pureut.system.util.FileConstant;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description 半成品控制层
 * @Param
 * @Date 22-11-25
 * @Author hl
 **/
@RestController
@RequestMapping("/half")
public class MaterialHalfController extends BaseController {

    @Resource
    private IMaterialHalfService materiaHalfService;

    @Resource
    private IMaterialCodingService materialCodingService;


    /**
     * 获取半成品配置
     */
    @RequiresPermissions("material:partially:list")
    @GetMapping("/list")
    public TableDataInfo list(MaterialHalfVo materialHalf)
    {
        startPage();
        List<MaterialHalfDto> list = materiaHalfService.selectHalfList(materialHalf);
        return getDataTable(list);
    }

    /**
     * 新增类型
     */
    @RequiresPermissions("material:partially:addType")
    @Log(title = "半成品类型", businessType = BusinessType.INSERT)
    @PostMapping("/addType")
    public AjaxResult add(@RequestBody MaterialCoding materialCoding) throws Exception {
        return toAjax(materialCodingService.codingAdd(materialCoding));
    }

    /**
     * 修改类别
     */
    @RequiresPermissions("material:partially:editType")
    @Log(title = "半成品类型", businessType = BusinessType.UPDATE)
    @PutMapping("/editType")
    public AjaxResult editType(@RequestBody MaterialCoding materialCoding) throws Exception {
        return toAjax(materialCodingService.updateCodingEdit(materialCoding));
    }

    /**
     * 删除类别
     */
    @RequiresPermissions("material:partially:removeType")
    @Log(title = "半成品类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeType")
    public AjaxResult remove(long codingId) throws Exception {
        return toAjax(materialCodingService.deleteCodingById(codingId));
    }

    /**
     * 新增半成品信息
     */
    @RequiresPermissions("material:partially:add")
    @Log(title = "半成品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MaterialHalf materialHalf) throws Exception {
        return toAjax(materiaHalfService.insertMaterialHalf(materialHalf));
    }

    /**
     * 删除半成品信息
     */
    @RequiresPermissions("material:partially:remove")
    @Log(title = "半成品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialId}")
    public AjaxResult remove(@PathVariable("materialId") String materialId) throws Exception {
        return toAjax(materiaHalfService.deleteMaterialHalf(materialId));
    }

    /**
     * 根据编号获取详细信息
     */
    //@RequiresPermissions("material:partially:query")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getHalfInfo(@PathVariable long materialId)
    {
        AjaxResult ajax = AjaxResult.success();
        MaterialHalfDto dto= materiaHalfService.selectMaterialHalfById(materialId);
        ajax.put("detailed", dto);
        return ajax;
    }

    /**
     * 修改半成品信息
     */
    @RequiresPermissions("material:partially:edit")
    @Log(title = "半成品信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody MaterialHalf materialHalf) throws Exception {
        return toAjax(materiaHalfService.updatMaterialHalfEdit(materialHalf));
    }


    /**
     * 半成品冻结
     */
    @RequiresPermissions("material:partially:freeze")
    @Log(title = "半成品信息", businessType = BusinessType.FREEZE)
    @PutMapping("/editFreeze")
    public AjaxResult editFreeze(@RequestBody MaterialHalf materialHalf){
        return toAjax(materiaHalfService.updateMaterialHalfEditFreeze(materialHalf));
    }

    /**
     * 半成品解冻
     */
    @RequiresPermissions("material:partially:thaw")
    @Log(title = "半成品信息", businessType = BusinessType.thaw)
    @PutMapping("/editThaw")
    public AjaxResult editThaw(@RequestBody MaterialHalf materialHalf){
        return toAjax(materiaHalfService.updateMaterialHalfEditFreeze(materialHalf));
    }

    /**
     * 半成品树形结构展示
     */
    @GetMapping("/listChildren")
    public AjaxResult listChildren()
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("theDrop", materiaHalfService.selectChildrenHalfListThe());
        return ajax;
    }


    /**
     * 根据物料编码查询详情(半成品信息)
     */
    @GetMapping("/selectMaterialHalfMaterialCode")
    public AjaxResult selectMaterialHalfMaterialCode(String materialCode) {
        return AjaxResult.success(materiaHalfService.selectMaterialHalfMaterialCode(materialCode));
    }

    /**
     * 根据物料编码查询详情(委外订单明细列表fegin调用)
     */
    @GetMapping("/getHalfViewByMaterialCode")
    public AjaxResult getHalfViewByMaterialCode(String materialCode) {
        return AjaxResult.success(materiaHalfService.getHalfViewByMaterialCode(materialCode));
    }

    /**
     * 半成品信息模板下载
     * @param response
     * @throws IOException
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        DownTemplateUtil.downTemplate(response, FileConstant.MATERIAL_HALF_IMPORT);
    }

    /**
     * 半成品信息导入
     */
    @PostMapping("/awmateriaImport")
    @RequiresPermissions("material:partially:import")
    @Log(title = "半成品信息导入", businessType = BusinessType.INSERT)
    public AjaxResult mouldMessageImport(MultipartFile file) throws Exception {
        ExcelUtil<MaterialHalfExport> excelUtil = new ExcelUtil<MaterialHalfExport>(MaterialHalfExport.class);
        List<MaterialHalfExport> coCustomer = excelUtil.importExcel(file.getInputStream());
        return AjaxResult.success(materiaHalfService.importMouldData(coCustomer));
    }

}
